package com.tianlu.store.controller;

import com.tianlu.store.common.ResponseResult;
import com.tianlu.store.entity.ProductInfo;
import com.tianlu.store.service.ShoppingCarService;
import com.tianlu.store.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @Author 梁东烂
 * @Desc 购物车表(ShoppingCar)表控制层
 * @Date 2023-04-27 14:07:33
 */
@Api(tags = "购物车表")
@RestController
@RequestMapping("/api/shoppingCar")
@RequiredArgsConstructor
public class ShoppingCarController {
    @Resource
    private ShoppingCarService shoppingCarService;

    @ApiOperation("查询购物车")
    @PostMapping("/findProductList")
    public ResponseResult<List<ProductInfo>> findProductList() {
        Long userId = JwtUtils.getIdByJwtToken();
        return ResponseResult.success(shoppingCarService.findProductList(userId));
    }

    @ApiOperation("修改购物车商品数量（加入购物车）")
    @PostMapping("/updateProductQuantity")
    public ResponseResult<Boolean> updateProductQuantity(@RequestParam Long productId, @RequestParam Integer quantity) {
        Long userId = JwtUtils.getIdByJwtToken();
        shoppingCarService.updateProductQuantity(productId, quantity, userId);
        return ResponseResult.success(true);
    }

    @ApiOperation("从购物车中删除")
    @PostMapping("/deleteProduct")
    public ResponseResult<Boolean> deleteProduct(@RequestParam Long productId) {
        Long userId = JwtUtils.getIdByJwtToken();
        shoppingCarService.deleteProduct(Collections.singletonList(productId), userId);
        return ResponseResult.success(true);
    }

    @ApiOperation("清空购物车")
    @PostMapping("/cleanUp")
    public ResponseResult<Boolean> cleanUp() {
        Long userId = JwtUtils.getIdByJwtToken();
        shoppingCarService.cleanUp(userId);
        return ResponseResult.success(true);
    }

}
